﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class my_new_edit : AdminpageBase
{
    protected string strtitle = ReadRequest.RequestString("title");
       protected string  ctype = ReadRequest.RequestString("t");
       protected long type = ReadRequest.RequestLong("type");
       protected long id = ReadRequest.RequestLong("id"); 
    protected Model.NewsSite model=new Model.NewsSite(); 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            inidata();      
        }
    }
    private void inidata()
    { 
        try
        {           
            if (ctype != null && "edit".Equals(ctype))
            {
                model = BLL.NewsSite.GetModel(id);
                if (model != null)
                {
                    texteid.Text = id.ToString();
                    txtTitle.Text = model.Title;
                    txtContent.Value = model.Contents;
                    //中奖说明
                    if (type == 4)
                    {
                        caipiaoType.SelectedValue = model.IsShow.ToString();
                    }
                }
            }
            else
            {
                ctype = "ad";
            }
        }
        catch {
            ctype = "ad";
        } 
    }

    //提交按钮事件
    protected void btn_submit_Click(object sender, EventArgs e)
    {
        save_Newscont();
    }
    private void save_Newscont()
    {
        //获取登录信息
        Model.Manageadmin loginUserInfo_cur = BLL.CMember.ManageadminLoginInfo();
        //没有登录
        if (loginUserInfo_cur == null)
        {
            alertmessageAndGoback("保存失败");
            return;
        } 
        bool isadd = true;
        if (ctype != null && "ad".Equals(ctype.ToLower()))
        {
            isadd = true;
        }
        else
        {
            isadd = false;
        }
        if (isadd)
        {

            Model.NewsSite model = new Model.NewsSite();
            model.Type = type;
            model.Title = txtTitle.Text;
            model.Contents = txtContent.Value;
            //中奖说明
            if (type == 4)
            {
                model.IsShow = int.Parse(caipiaoType.SelectedValue);
            }
            model.AddTime = DateTime.Now;
            if (BLL.NewsSite.Add(model) > 0)
            {
                alertmessageAndGo("保存成功！", "/manage/my_newsSite.aspx?type=" + model.Type + "&title=" + strtitle);
                return;
            }
            else
            {
                alertmessageAndGoback("保存失败");
                return;
            }
        }
        else
        {
            Model.NewsSite model = BLL.NewsSite.GetModel(id);
            if (model != null)
            {
                model.Title = txtTitle.Text;
                model.Contents = txtContent.Value;
                if ((new DateTime()).Equals(model.AddTime))
                {
                    model.AddTime = DateTime.Now;
                }

                if (BLL.NewsSite.Update(model) > 0)
                {
                    alertmessageAndGo("保存成功！", "/manage/my_newsSite.aspx?type=" + model.Type + "&title=" + strtitle);
                    return;
                }
                else
                {
                    alertmessageAndGoback("保存失败");
                    return;
                }
            }
        }
    }
}